//
//  AddressesViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/30.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "AddressesViewController.h"

#import "AFNetworking.h"
#import "URL.h"
#import "AppDelegate.h"
@interface AddressesViewController ()<UIPickerViewDelegate,UITextFieldDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)NSMutableArray * pickArray;

@property (weak, nonatomic) IBOutlet UIView *backgroundView;

@property (weak, nonatomic) IBOutlet UIButton *pickViewButton;

@property (strong, nonatomic)  UIPickerView *pickView;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@end
@implementation AddressesViewController
-(void)viewDidLoad{
    
    [super viewDidLoad];
    self.title=@"常用地址";
    [self setupUI];
    [self loadData];
    
    
}

-(void)setupUI{
    
    self.backgroundView.layer.cornerRadius=5;
    self.backgroundView.layer.borderColor=[UIColor groupTableViewBackgroundColor].CGColor;
    self.backgroundView.layer.borderWidth=1;
    
   // _pickArray=@[@"南城国际航空港",@"深圳宝安国际机场",@"新郑国际机场",@"珠江路新世界中心",@"油坊桥",@"建邺区",@"新街口苏苑大厦"];
    
    self.textField.delegate=self;
    self.textField.tag=1001;
    self.pickView=[[UIPickerView alloc]init];
    CGRect frame=[UIScreen mainScreen].bounds;
    
    self.pickView.frame=CGRectMake(0, frame.size.height, frame.size.width, 216);
    
    self.pickView.delegate=self;
    self.pickView.dataSource=self;
    self.textField.inputView=self.pickView;
   // self.pickView.showsSelectionIndicator=YES;
    

    [self.view addSubview:self.pickView];
}


//-(NSArray*)array{
//    if (!_pickArray) {
//        
//        _pickArray=[[NSArray alloc]init];
//        
//    }
//    return _pickArray;
//    
//    
//}

#pragma loadData

-(void)loadData{
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    AppDelegate* tempAppdelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSDictionary * paranter=[[NSDictionary alloc]init];
    paranter=@{@"token":tempAppdelegate.token};
    NSString * urlString=[NSString stringWithFormat:@"%@%@",url1,@"GetChangePoint"];
    [manager POST:urlString parameters:paranter progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic=(NSDictionary*)responseObject;
      //  NSLog(@"%@",[[dic valueForKey:@"Data"] valueForKey:@"Name"]);
        
        self.pickArray=[NSMutableArray arrayWithArray:[[dic valueForKey:@"Data"] valueForKey:@"Name"]];
        
        NSLog(@"self.pickArray:%@",self.pickArray );
        
      // self.pickArray=[[dic valueForKey:@"Data"] valueForKey:@"Name"];
        //NSLog(@"地址%@",self.pickArray);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"Error:%@",error);
        
    }];
    
}
#pragma ------------------点击button出现pickView------------------------

- (IBAction)pickViewAppear:(id)sender {
    
    UIButton * button=sender;
    if (button.selected) {
        
        [UIView animateWithDuration:1 animations:^{
            
            CGRect frame=[UIScreen mainScreen].bounds;
            
            self.pickView.frame=CGRectMake(0, frame.size.height, frame.size.width, 216);

        }];

    }else{
        [self.pickView becomeFirstResponder];
        [UIView animateWithDuration:1 animations:^{
            CGRect frame=[UIScreen mainScreen].bounds;
            
            self.pickView.frame=CGRectMake(0, frame.size.height-216, frame.size.width, 216);
            [self.pickView reloadAllComponents];
            
        }];
        
    }
    button.selected=!button.selected;
    
}

#pragma --------------pickView----------------------
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    
    return self.pickArray.count;
    
    
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    
    
    return self.pickArray[row];

    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    self.textField.text=self.pickArray[row];
    
    
    
    
    
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    NSInteger row =[_pickView selectedRowInComponent:0];
    
    self.textField.text=[self.pickArray objectAtIndex:row];
    
    
}
#pragma -----------点击textfield--------------------
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (textField.tag!=1001) {
        
        if (self.pickView.superview) {
            
            [self.pickView removeFromSuperview];
            
        }
        return YES;
        
    }
    if (self.pickView.superview==nil) {
        
        [self.pickView becomeFirstResponder];
        
        [UIView animateWithDuration:1 animations:^{
            
            CGRect frame=[UIScreen mainScreen].bounds;
            
            self.pickView.frame=CGRectMake(0, frame.size.height-216, frame.size.width, 216);
            [self.view addSubview:self.pickView];
            
        }];
    }
    return NO;
}
@end
